function unit() {
  var setCookie = function(name, value) {
    document.cookie = name + "=" + value;
  };
  var getCookie = function(cookieName) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
      var arr = arrCookie[i].split("=");
      if (cookieName == arr[0]) {
        return arr[1];
      }
    }
    return "";
  };
  var getAll = function(res) {
    // console.log(res)
    // return false
    // return res.data;
    let userinfo = res.data.data[0];
    // console.log(userinfo);
    // return res.data;
    // return userinfo;
    this.TmpDate1 = userinfo.date1; //购车
    this.TmpDate = userinfo.date; //买保险
    this.TmpDate2 = userinfo.date_sc; //审车
    // 修复错误
    userinfo.bx_true =
      userinfo.bx_true == "true" || userinfo.bx_true == true ? true : false;
    userinfo.sc_true =
      userinfo.sc_true == "true" || userinfo.sc_true == true ? true : false;
    userinfo.date = userinfo.date == "null" ? "" : userinfo.date;
    userinfo.date1 = userinfo.date1 == "null" ? "" : userinfo.date1;
    userinfo.date_sc = userinfo.date_sc == "null" ? "" : userinfo.date_sc;
    if (userinfo.buy == "true" || userinfo.buy == true) {
      userinfo.buy = true;
    } else {
      userinfo.buy = false;
    }
    return userinfo;
  };
  var reimg = function(reimg, e) {
    let url = getCookie("url");
    // console.log(reimg, e);
    if (reimg.img != null) {
      reimg.img = url + e + reimg.img.substring(reimg.img.lastIndexOf("/"));
    }
    if (reimg.img_xszfm != null) {
      reimg.img_xszfm =
        url + e + reimg.img_xszfm.substring(reimg.img_xszfm.lastIndexOf("/"));
    }
    if (reimg.img_qs != null) {
      reimg.img_qs =
        url + e + reimg.img_qs.substring(reimg.img_qs.lastIndexOf("/"));
    }
    if (reimg.img_new != null) {
      reimg.img_new =
        url + e + reimg.img_new.substring(reimg.img_new.lastIndexOf("/"));
    }
    if (reimg.img_jsz != null) {
      reimg.img_jsz =
        url + e + reimg.img_jsz.substring(reimg.img_jsz.lastIndexOf("/"));
    }
    return reimg;
  };
  var aAndZ = function() {
    let big = [];
    for (let i = 65; i < 91; i++) {
      big.push(String.fromCharCode(i));
    }
    return big;
  };
  var Num = function() {
    let num = [];
    for (var i = 0; i < 10; i++) {
      num.push(i);
    }
    return num;
  };
  let formData = new FormData();
  var edidImg = function(imgName, oldimg, newImg, _id, axios, message) {
    formData.append("imgName", imgName); //图片字段名
    formData.append("oldimg", oldimg); //旧图地址
    formData.append("newImg", newImg); //新图地址
    // console.log(newImg);
    formData.append("_id", _id);
    axios({
      method: "POST",
      url: "/api/editimg",
      data: formData,
    }).then((res) => {
      if (res.data.status === 1) {
        message({
          message: "修改成功!",
          type: "success",
        });
      }
    });
  };
  var sort_ = function(table) {
    let temp = [];
    let temp2 = [];
    // console.log(table)
    table.forEach((e, i) => {
      if (e.date != "") {
        temp.push(e);
      } else {
        temp2.push(e);
      }
    });
    function sortDay(a, b) {
      //a-b为升序，b-a为降序
      let c = a.date.replace(a.date.slice(0, 4), "2022");
      let d = b.date.replace(b.date.slice(0, 4), "2022");
      return Date.parse(c) - Date.parse(d);
    }
    table = temp.sort(sortDay);
    table.push.apply(table, temp2);
    return table;
  };
  /**
   * table 数据
   * name 购保还是审车
   * areaBox 折线图数据
   * k 第几根线 --1
   *
   */
  var ech = function(table, name, areaBox, k) {
    let date = new Date();
    // console.log(date.getFullYear());
    let year = date.getFullYear();
    if (name == "date_sc") {
      //审车

      table.forEach((e, i) => {
        if (
          (e[name] != "" && year - e[name].slice(0, 4) >= 2) ||
          year - e[name].slice(0, 4) == 0
        ) {
          // console.log(e[name])
          // console.log(year - e[name].slice(0, 4))
          if (k == 0) {
            let mouth = e[name].slice(5, 7).replace("/", "");
            switch (Number(mouth)) {
              case Number(mouth):
                areaBox.series[k].data[Number(mouth) - 1] += 1;
                break;
            }
          } else if (k == 1) {
            if (e.sc_true === "true" || e.sc_true === true) {
              let mouth = e[name].slice(5, 7).replace("/", "");
              switch (Number(mouth)) {
                case Number(mouth):
                  areaBox.series[k].data[Number(mouth) - 1] += 1;
                  break;
              }
            }
            // b = 0;
          } else if (k == 2 && year - e[name].slice(0, 4) >= 4) {
            if (e.sc_true === "true" || e.sc_true === true) {
              let mouth = e[name].slice(5, 7).replace("/", "");
              // console.log(mouth)
              switch (Number(mouth)) {
                case Number(mouth):
                  areaBox.series[k].data[Number(mouth) - 1] += 1;
                  break;
              }
            }
          }
        }
      });
    } else if (name == "date") {
      table.forEach((e, i) => {
        if (k == 0) {
          let mouth = e[name].slice(5, 7).replace("/", "");
          switch (Number(mouth)) {
            case Number(mouth):
              areaBox.series[k].data[Number(mouth) - 1] += 1;
              break;
          }
        } else if ((k == 1 && e.bx_true == "true") || e.bx_true == true) {
          let mouth = e[name].slice(5, 7).replace("/", "");
          switch (Number(mouth)) {
            case Number(mouth):
              areaBox.series[k].data[Number(mouth) - 1] += 1;
              break;
          }
        }
      });
    }
    return areaBox;
  };

  return {
    setCookie: setCookie,
    getCookie: getCookie,
    getAll: getAll, //获取用户
    aAndZ: aAndZ,
    Num: Num, //模拟选牌
    reimg: reimg, // 上传图片
    edidImg: edidImg, //修改图片
    sort_: sort_,
    ech: ech,
  };
}

module.exports = unit();
